package com.niit.controller;

import com.niit.pojo.*;
import com.niit.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Controller
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private PlaceService placeService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private UnitService unitService;
    @Autowired

    private StoreService storeService;
    @Autowired
    private SupplyService supplyService;
    @RequestMapping("/listProduct")
    public String productListPage(Model model) {
        List<ProductView> productList=productService.productList();
        System.out.println("productListPage"+productList);
        model.addAttribute("productList",productList);
        return "/listProduct";
    }

    @RequestMapping("/addProduct")
    public String addProduct(ProductView productView){
        System.out.println("addProduct"+productView);
        System.out.println(productView.getUnit_name());
        int supply_id=supplyService.selectIdByName(productView.getSupply_name());
        int unit_id=unitService.selectIdByName(productView.getUnit_name());
        int place_id=placeService.selectIdByName(productView.getPlace_name());
        int type_id= typeService.selectIdByName(productView.getType_name());
        int brand_id= brandService.selectIdByName(productView.getBrand_name());
        int store_id= storeService.selectIdByName(productView.getStore_name());
        Product product=new Product(productView.getProduct_id(),
                productView.getProduct_name(),
                store_id,brand_id,type_id,place_id,
                productView.getProduct_num(),unit_id,
                productView.getIntroduce(),productView.getIn_price(),
                productView.getSale_price(),productView.getUp_down_state(),supply_id);
        productService.addProduct(product);
        return "redirect:/listProduct";
    }
    @RequestMapping("/toAddProduct")
    public ModelAndView toAddProduct(){
        System.out.println("toAddProduct");
        ModelAndView modelAndView=new ModelAndView("/addProduct");
        List<Supply> supplyList=supplyService.selectAllSupply();
        List<Brand> brands=brandService.selectAllBrand();
        List<Place> places=placeService.selectAllPlace();
        List<ProductType> types=typeService.selectAllType();
        List<Unit> units=unitService.getAllUnits();
        List<Store> stores=storeService.selectAllStore();
        for (Brand brand : brands) {
            System.out.println(brand);
        }
        modelAndView.addObject("supplyList",supplyList);
        modelAndView.addObject("brands",brands);
        modelAndView.addObject("places",places);
        modelAndView.addObject("types",types);
        modelAndView.addObject("units",units);
        modelAndView.addObject("stores",stores);
        System.out.println("toAddProduct");

        return modelAndView;
    }

    @RequestMapping("/deleteProduct")
    public String deleteProduct(@RequestParam("product_id") String id){
        int product_id= Integer.valueOf(id);
        productService.deleteProductById(product_id);
        return "redirect:/listProduct";
    }


    @RequestMapping("/updateProduct")
    public String updateProduct(ProductView productView){
        System.out.println("updateProduct++++++"+productView);
        System.out.println("updateProduct"+productView.getProduct_name());
        int supply_id=supplyService.selectIdByName(productView.getSupply_name());
        int unit_id=unitService.selectIdByName(productView.getUnit_name());
        int place_id=placeService.selectIdByName(productView.getPlace_name());
        int type_id= typeService.selectIdByName(productView.getType_name());
        int brand_id= brandService.selectIdByName(productView.getBrand_name());
        int store_id= storeService.selectIdByName(productView.getStore_name());
        Product product=new Product(productView.getProduct_id(),
                productView.getProduct_name(),
                store_id,brand_id,type_id,place_id,
                productView.getProduct_num(),unit_id,
                productView.getIntroduce(),productView.getIn_price(),
                productView.getSale_price(),productView.getUp_down_state(),supply_id);
        productService.updateProduct(product);
        System.out.println(product);
        return "redirect:/listProduct";

    }
    @RequestMapping("/toUpdateProduct")
    public ModelAndView toUpdateProduct(@RequestParam("product_id") Integer id){
        System.out.println("toUpdateProduct"+id);
        ModelAndView modelAndView=new ModelAndView("/updateProduct");
        List<Supply> supplyList=supplyService.selectAllSupply();
        List<Brand> brands=brandService.selectAllBrand();
        List<Place> places=placeService.selectAllPlace();
        List<ProductType> types=typeService.selectAllType();
        List<Unit> units=unitService.getAllUnits();
        List<Store> stores=storeService.selectAllStore();
        for (Brand brand : brands) {
            System.out.println(brand);
        }
        modelAndView.addObject("supplyList",supplyList);
        modelAndView.addObject("brands",brands);
        modelAndView.addObject("places",places);
        modelAndView.addObject("types",types);
        modelAndView.addObject("units",units);
        modelAndView.addObject("stores",stores);

        ProductView productView=productService.selectProductViewById(id);

        modelAndView.addObject("productView",productView);
        System.out.println("toUpdateProduct"+productView);
        return modelAndView;
    }



    @RequestMapping("/toSelectProduct")
    public String selectProduct(@RequestParam("search") String search,Model model){
        System.out.println("selectProduct");
        System.out.println("search"+search);
        List<ProductView> productList=productService.selectProduct(search);
        model.addAttribute("productList",productList);
        System.out.println("productList select"+productList);
        return "/selectProduct";
    }

}
